<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules" label-position="right">
      <el-form-item label="广告名称" prop="title">
        <el-input v-model.trim="form.title" />
      </el-form-item>
      <el-form-item label="跳转地址" prop="url">
        <el-input v-model.trim="form.url" />
      </el-form-item>
      <el-form-item v-if="title!='添加'" label="广告图片">
        <el-image :src="fileRootPath+form.pic" style="width:100%;height:auto" />
      </el-form-item>
      <el-form-item label="更改图片" style="height:180px;margin-bottom: 30px">
        <Upload ref="upload" typ="banner" @input="changeUrl" />
        <p class="tips-words">ps：建议上传jpeg/jpg/png文件，且不超过10M</p>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>

  import {doEdit  } from '@/api/manage/ad'
  import Upload from '@/views/other/upload/SingleImage3'

  export default {
    name: 'adManagementEdit',
    components: { Upload },
    data() {
      return {
        form: {
        },
        rules: {
          title: [
            { required: true, trigger: 'blur', message: '请填写广告名称' },
          ],
          url:[
            { type: 'url', trigger: 'blur', message: '请输入链接标准地址(http/https)'},
          ]
        },
        fileRootPath:'',
        title: '',
        dialogFormVisible: false,
        columnList:[],
        infoList:[],
        statusMap:[
          {
            status:0,
            text:'禁用'
          },
          {
            status:1,
            text:'启用'
          }
        ],
        targetMap:[
          {
            target:'_blank',
            text:'新页面打开'
          },
          {
            target:'_self',
            text:'当前页打开'
          }
        ],
        sonMap:[
          {
            son:1,
            text:'栏目列表'
          },
          {
            son:2,
            text:'栏目广告'
          }
        ],
        typeMap:[
          {
            type:1,
            text:'栏目'
          },
          {
            type:2,
            text:'单页'
          },
          {
            type:3,
            text:'固定连接'
          }
        ]

      }
    },
    created(){
      this.getFilter()
      this.fileRootPath=process.env.VUE_APP_FILE_ROOT_PATH
    },
    methods: {
      changeUrl(data) {
        this.form.tempPath = data.tempPath
        this.form.fileType = data.fileType
      },
      forceUpdate(){
        this.$forceUpdate()
      },
      getFilter(){
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
          this.form.class_id=this.$route.query.class_id
          this.form.pic=''
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(this.form)
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>
